//
//  ScrollImageView.m
//  ClassifyShop
//
//  Created by yuyao on 15/3/9.
//  Copyright (c) 2015年 yuyao. All rights reserved.
//

#import "ScrollImageView.h"
#import "GUIDefine.h"
@implementation ScrollImageView
@synthesize array;
- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        [scrollView setTag:1000];
        [scrollView setDelegate:self];
        scrollView.pagingEnabled = YES;
        scrollView.showsHorizontalScrollIndicator = NO;
        [self addSubview:scrollView];
        
        UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, frame.size.height-35,frame.size.width, 35)];
        [pageControl setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.5]];
        [pageControl setTag:101];
        [pageControl setCurrentPage:0];
        [pageControl addTarget:self action:@selector(pageValueChanged:) forControlEvents:UIControlEventValueChanged];
        [self addSubview:pageControl];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 120, 15)];
        [label setTextColor:[UIColor whiteColor]];
        [label setTag:2000];
        [label setFont:[UIFont fontWithName:@"Arial" size:12]];
        [pageControl addSubview:label];
        
        UIImageView *shoucangImage = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width-50, 5, 25, 25)];
        [shoucangImage setTag:3000];
        [shoucangImage setImage:[UIImage imageNamed:@"7 收藏.png"]];
        [pageControl addSubview:shoucangImage];
    }
    return self;
}
- (void)setDatas:(NSArray *)datas{
    self.array = datas;
    UIScrollView *scrollView = (UIScrollView *)[self viewWithTag:1000];
    [scrollView setContentSize:CGSizeMake(self.frame.size.width*datas.count, self.frame.size.height)];
    UIPageControl *pageControl = (UIPageControl *)[self viewWithTag:101];
    [pageControl setNumberOfPages:datas.count];

    UILabel *label = (UILabel *)[self viewWithTag:2000];
    [label setText:[[datas objectAtIndex:0] objectForKey:@"title"]];

    for (int i = 0; i < datas.count; i++) {

        UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(i*self.frame.size.width, 0, self.frame.size.width, self.frame.size.height)];
        [imageview setImageWithURL:[NSURL URLWithString:[[datas objectAtIndex:i] objectForKey:@"photo"]]];
        [scrollView addSubview:imageview];
        
        UIImageView *timeImage = [[UIImageView alloc] initWithFrame:CGRectMake(i*self.frame.size.width+10, 10, 90, 30)];
        [timeImage setImage:[UIImage imageNamed:@"shoptime.png"]];
        [scrollView addSubview:timeImage];
        
        UIImageView *parkImage = [[UIImageView alloc] initWithFrame:CGRectMake((i+1)*self.frame.size.width-150, 10, 40, 40)];
        [parkImage setImage:[UIImage imageNamed:@"10 停车位.png"]];
        [scrollView addSubview:parkImage];
        
        UIImageView *roomImage = [[UIImageView alloc] initWithFrame:CGRectMake((i+1)*self.frame.size.width-100, 10, 40, 40)];
        [roomImage setImage:[UIImage imageNamed:@"11 包房.png"]];
        [scrollView addSubview:roomImage];
        
        UIImageView *time24Image = [[UIImageView alloc] initWithFrame:CGRectMake((i+1)*self.frame.size.width-50, 10, 40, 40)];
        [time24Image setImage:[UIImage imageNamed:@"12 24小时营业.png"]];
        [scrollView addSubview:time24Image];
        

    }
    [self bringSubviewToFront:pageControl];

}
- (void)pageValueChanged:(id)sender{
    UIPageControl *pc = (UIPageControl *)sender;
    UIScrollView *scrollView = (UIScrollView *)[self viewWithTag:1000];

    [scrollView scrollRectToVisible:CGRectMake(self.frame.size.width*pc.currentPage, 0, self.frame.size.width, self.frame.size.height) animated:YES];
    
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    currentPage = scrollView.contentOffset.x/self.frame.size.width;
    UIPageControl *pageControl = (UIPageControl *)[self viewWithTag:101];

    NSLog(@"currentPage = %d",currentPage);
    UILabel *label = (UILabel *)[self viewWithTag:2000];
    [label setText:[[self.array objectAtIndex:currentPage] objectForKey:@"title"]];
    [pageControl setCurrentPage:currentPage];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
